package cn.itcast.bos.action.base.take_delivery;

import java.util.HashMap;
import java.util.Map;

import javax.jms.JMSException;
import javax.jms.MapMessage;
import javax.jms.Message;
import javax.jms.Queue;
import javax.jms.Session;

import org.apache.log4j.Logger;
import org.apache.struts2.convention.annotation.Action;
import org.apache.struts2.convention.annotation.Namespace;
import org.apache.struts2.convention.annotation.ParentPackage;
import org.apache.struts2.convention.annotation.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Scope;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Sort;
import org.springframework.jms.core.JmsTemplate;
import org.springframework.jms.core.MessageCreator;
import org.springframework.stereotype.Controller;

import com.opensymphony.xwork2.ActionContext;

import cn.itcast.bos.domain.take_delivery.WayBill;
import cn.itcast.bos.service.base.WayBillService;
import cn.itcast.bos.web.action.common.BaseAction;

/**
 * 运单快速录入保存
 * 
 * @author 艾欣
 *
 */
@ParentPackage("json-default")
@Controller
@Scope("prototype")
@Namespace("/")
public class WayBillAction extends BaseAction<WayBill> {
	// 注入业务层
	@Autowired
	private WayBillService wbs;
	// 注入消息队列的jmsTemplate
	@Autowired
	private JmsTemplate jmsTemplate;
	// 注入消息目的地
	@Autowired
	private Queue queueDestination;
	// 导入日志文件
	private static final Logger logger = Logger.getLogger(WayBillAction.class);

	@Action(value = "waybill_save", results = { @Result(name = "success", type = "json") })
	public String save() {
		// 保存成功信息
		Map<String, Object> result = new HashMap<String, Object>();
		try {
			if (model.getOrder() != null
					&& (model.getOrder().getId() == null || model.getOrder()
							.getId() == 0)) {
				model.setOrder(null);
			}
			wbs.save(model);
			// 封装数据
			jmsTemplate.send(queueDestination, new MessageCreator() {

				@Override
				public Message createMessage(Session session)
						throws JMSException {
					MapMessage message = session.createMapMessage();
					message.setInt("id", model.getId());
					return message;
				}
			});
			result.put("success", true);
			result.put("msg", "保存运单成功");
			logger.info("保存运单成功,运单号:" + model.getWayBillNum());

		} catch (Exception e) {
			e.printStackTrace();
			// 保存失败
			result.put("success", false);
			result.put("msg", "保存运单失败");
			logger.error("保存运单失败,运单号:" + model.getWayBillNum());
		}
		return SUCCESS;

	}

	/**
	 * 页面查询
	 */
	@Action(value = "waybill_pageQuery", results = { @Result(name = "success", type = "json") })
	public String pageQuery() {
		// 直接查询所有
		Pageable pageable = new PageRequest(page - 1, rows, new Sort(
				new Sort.Order(Sort.Direction.ASC, "id")));
		// 调用业务层进行查询
		Page<WayBill> pageData = wbs.findPageData(model,pageable);
		// 压入值栈
		pushPageDataToValueStack(pageData);
		return SUCCESS;
	}
	/**
	 * 回显运单
	 */
	@Action(value="waybill_findByWayBillNum",results={@Result(name="success",type="json")})
	public String findByWayBillNum(){
		//调用业务层查询
		WayBill wayBill = wbs.findByWayBillNum(model.getWayBillNum());
		Map<String, Object> map = new HashMap<String, Object>();
		//判断运单是否存在记录结果
		if(wayBill==null){
			map.put("success", false);
		}else{
			//如果运单存在
			map.put("success", true);
			//存入运单
			map.put("wayBillData", wayBill);
		}
		//压入值栈
		ActionContext.getContext().getValueStack().push(map);
		return SUCCESS;
	}
}
